#include<stdio.h>
#include<string.h>
#include <assert.h>
#include <stdlib.h>


int isOK(int c)
{
    if( 'a' <= c && c <= 'z') return c;
    else if( 'A' <= c && c <= 'Z') return c - 'A' + 'a';
    else if( '0' <= c && c <= '9') return c;
    else if( c == '\n') return c;
    else if( c == ' ') return c;    
    else if( c ==  EOF) return c;
    return 0;
}

char isPolish(int c1, int c2)
{
    if( c1 == -60 && c2 == - 103) return '!';
    else if( c1 == -61 && c2 == - 77) return '@';
    else if( c1 == -60 && c2 == - 123) return '?';
    else if( c1 == -59 && c2 == - 101) return '$';
    else if( c1 == -59 && c2 == - 126) return '%';
    else if( c1 == -59 && c2 == - 68) return '^';
    else if( c1 == -59 && c2 == - 70) return '&';
    else if( c1 == -60 && c2 == - 121) return '*';
    else if( c1 == -59 && c2 == - 124) return '(';
    else if( c1 == -60 && c2 == - 104) return '!'; //duże litery!
    else if( c1 == -61 && c2 == - 109) return '@';
    else if( c1 == -60 && c2 == - 124) return '?';
    else if( c1 == -59 && c2 == - 102) return '$';
    else if( c1 == -59 && c2 == - 127) return '%';
    else if( c1 == -59 && c2 == - 69) return '^';
    else if( c1 == -59 && c2 == - 71) return '&';
    else if( c1 == -60 && c2 == - 122) return '*';
    else if( c1 == -59 && c2 == - 125) return '(';    
    else if( c1 == '#' && c2 == '#') return '#';
    else if( c1 == -61 && c2 == -68) return '>';
    else return 0;
}


#define bufSize 128 * 1024

inline int myGetc( FILE *file ){ //btw getc zwraca inta
    static char buf[bufSize];
    static int it = bufSize;
    if ( it >= bufSize ){
        int count = fread ( buf, 1, bufSize, file );
        while ( count < bufSize ) buf[count++] = EOF;
        it = 0;
    }
    if ( buf[it] == EOF ){
        it = bufSize;
        return EOF;
    }
    return buf[it++];
}

inline void myPutc( FILE *file, int c ){
    static char buf[bufSize];
    static int it = 0;
    if ( c == EOF ) {
        fwrite ( buf, 1, it, file );
        it = 0;
        return;
    }
    buf[it++] = c;
    if ( it >= bufSize ){
        fwrite ( buf, 1, bufSize, file );
        it = 0;
    }
}



void convert(char* from, char * to, int flag)
{

    FILE* input_file = fopen(from, "r");
    if( !input_file )
    {   
        printf("plik %s nie istnieje\n",from);
    }
    FILE* output_file = fopen(to,"w");
    int c = myGetc(input_file);
    int b;
    while( c != EOF )
    {
        if( isOK(c) > 0)
        {
            myPutc ( output_file, isOK(c) );
            //fprintf(output_file,"%c",isOK(c) );
            c = myGetc(input_file);
        }
        else
        {
            b = myGetc(input_file);
            if( isPolish(c,b) != 0)
            {
                myPutc ( output_file, isPolish(c,b) );
                //fprintf(output_file,"%c", isPolish(c,b) );
                c = myGetc(input_file);
            }
            else
            {
               if( flag == 0) myPutc( output_file, ' ' );
//                   fprintf(output_file," ");
                   //fprintf(output_file,"%c",c)
               c = b;
            }
        }
    }
    myPutc ( output_file, EOF ); //< konieczne, bo końcówka się nie zapisze
    fclose ( input_file );
    fclose ( output_file );
}

int main()
{
    convert("wiki.txt","wiki2.txt",0);
    convert("morfo.txt","morfo2.txt",1);    
}



